package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Booking;
import com.example.demo.mapper.BookingMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/booking")
public class BookingController {
    @Resource
    BookingMapper bookingMapper;

    @PostMapping("/add")
    public Result<?> add(@RequestBody Booking booking){
        bookingMapper.insert(booking);
        return Result.success();
    }

    @GetMapping("/data")
    public Result<?> test(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String search){
        //orderByDesc(Booking::getBkSumbitDate) 根据提交时间降序排列
        LambdaQueryWrapper<Booking> wrapper = Wrappers.<Booking>lambdaQuery().orderByDesc(Booking::getBkSumbitDate);
        if (StrUtil.isNotBlank(search)) wrapper.like(Booking::getBkName,search);
        Page<Booking> userPage = bookingMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }
}
